package com.njsf.service.impl;

<<<<<<< HEAD
=======
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
import com.njsf.dao.CartMapper;
import com.njsf.dao.OrderDetailMapper;
import com.njsf.dao.OrderMapper;
import com.njsf.dto.ResponseJson;
<<<<<<< HEAD
import com.njsf.excepiton.BusinessException;
import com.njsf.pojo.CartPlus;
import com.njsf.pojo.Order;
import com.njsf.pojo.OrderDetail;
import com.njsf.pojo.OrderDetailPlus;
=======
import com.njsf.pojo.*;
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
import com.njsf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private CartMapper cartMapper;

    /**创建订单
     * @param cartPluses
     * @param userId
     * @return
     */
    @Override
    @Transactional
<<<<<<< HEAD
    public ResponseJson createOrder(List<CartPlus> cartPluses,Integer userId) throws Exception{

        String orderNumber = null;
        try {
            float totalMoney = 0;//订单总价
            for (CartPlus c:cartPluses){
                totalMoney+=c.getTotal();
            }
            Order order = new Order();//订单记录
            orderNumber = UUID.randomUUID().toString().replace("-", "");
            order.setOrderNumber(orderNumber);
            order.setOrderTime(new Date());
            order.setState(0);
            order.setTotalMoney(totalMoney);
            order.setUserId(userId);
            int insert = orderMapper.insert(order);

            List<OrderDetail> orderDetails = new ArrayList<>();
            for (CartPlus c:cartPluses){
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setOrderNumber(orderNumber);//订单编号
                orderDetail.setBookId(c.getBookId());
                orderDetail.setNumb(c.getNumb());
                orderDetail.setTotal(c.getTotal());
                orderDetails.add(orderDetail);
            }
            /*批量插入*/
            orderDetailMapper.batchInsert(orderDetails);

            /*删除用户购物车中信息*/
            cartMapper.deleteByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("服务器错误！");
        }
=======
    public ResponseJson createOrder(List<CartPlus> cartPluses,Integer userId) {

        float totalMoney = 0;//订单总价
        for (CartPlus c:cartPluses){
            totalMoney+=c.getTotal();
        }
        Order order = new Order();//订单记录
        String orderNumber = UUID.randomUUID().toString().replace("-", "");
        order.setOrderNumber(orderNumber);
        order.setOrderTime(new Date());
        order.setState(0);
        order.setTotalMoney(totalMoney);
        order.setUserId(userId);
        int insert = orderMapper.insert(order);

        List<OrderDetail> orderDetails = new ArrayList<>();
        for (CartPlus c:cartPluses){
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderNumber(orderNumber);//订单编号
            orderDetail.setBookId(c.getBookId());
            orderDetail.setNumb(c.getNumb());
            orderDetail.setTotal(c.getTotal());
            orderDetails.add(orderDetail);
        }
        /*批量插入*/
        orderDetailMapper.batchInsert(orderDetails);

        /*删除用户购物车中信息*/
        cartMapper.deleteByUserId(userId);
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8

        return new ResponseJson(100,"订单已生成!",orderNumber);
    }

    /**
     * @param
     * @return
     */
    @Override
    public List<OrderDetailPlus> findOrderDetais(String orderNumber) {
        List<OrderDetailPlus> detailsByOrderId = orderDetailMapper.findDetailsByOrderNumber(orderNumber);
        return detailsByOrderId;
    }
<<<<<<< HEAD
=======


    @Override
    public ResponseJson updateUserInfo(String phone, String address,String orderNumber) {
        ResponseJson responseJson = null;
        int flag = orderMapper.updateOrderInfo(phone, address,orderNumber);
        if (flag !=0){
            responseJson = new ResponseJson(100,"更新信息成功");
        }else {
            responseJson = new ResponseJson(101,"更新信息失败");
        }
        return responseJson;
    }

    @Override
    public PageInfo<OrderListLevel1> getOrders(Integer start, Integer pageSize) {
        PageHelper.startPage(start,pageSize);
        List<OrderListLevel1> all = orderMapper.findAll();
        PageInfo<OrderListLevel1> pageInfo = new PageInfo<>(all);
        return pageInfo;
    }

    @Override
    public PageInfo<OrderListLevel2> getOneUserOrders(Integer start, Integer pageSize, Integer id) {
        PageHelper.startPage(start,pageSize);
        List<OrderListLevel2> allByOneUser = orderMapper.findAllByOneUser(id);
        PageInfo<OrderListLevel2> pageInfo = new PageInfo<>(allByOneUser);
        return pageInfo;
    }
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
}
